﻿package com.thaiflashdev.utils 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.filters.*;
	
	/**
	 * Utility class for insert one or more filters to indicated MovieClip
	 * @author korstudioX2
	 * @version 0.1
	 * 
	 * TODO: Add more filters
	 */
	public class EasyFilter 
	{
		
		public function EasyFilter() 
		{
			throw new Error("com.korstudio.utils.EasyFilter => This class cannot be initialized.");
		}
		
		/**
		 * Add a glow filter to specified display object
		 * @param	target	Target display object
		 * @param	color	Color of glow effect [default: 0x000000]
		 * @param	blurX	Blur amount on X axis [default: 5]
		 * @param	blurY	Blur amount on Y axis [default: 5]
		 * @param	alpha	Opacity of glow effect [default: 1]
		 * @param	strength	Strength of glow effect [default: 2]
		 * @param	quality		Quality of glow effect (1-3) [default: 1]
		 * @param	innerGlow	Set whether this is an inner or outer glow (true/false) [default: false]
		 */
		public static function Glow(target:DisplayObject, color:uint=0x000000, blurX:Number=5, blurY:Number=5, alpha:Number=1, strength:Number=2, quality:int=1, innerGlow:Boolean=false):void {
			var glowFilter:GlowFilter = new GlowFilter(color, alpha, blurX, blurY, strength, quality);
			glowFilter.inner = innerGlow;
			assignFilter(target, glowFilter);
		}
		
		/**
		 * Adjust brightness of targeted display object
		 * @param	target	Targeted display object
		 * @param	value	Brightness (-100 to 100; Normal is 0)
		 */
		public static function Brightness(target:DisplayObject, value:Number):void {
			var matrix:Array = new Array();
            matrix = matrix.concat([1, 0, 0, 0, value]); // red
            matrix = matrix.concat([0, 1, 0, 0, value]); // green
            matrix = matrix.concat([0, 0, 1, 0, value]); // blue
            matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha

			var colorMatrixBrightness:ColorMatrixFilter = new ColorMatrixFilter(matrix);
			assignFilter(target, colorMatrixBrightness);
		}
		
		/**
		 * Add drop shadow filter to targeted display object
		 * @param	target	Display object to be added the filter
		 * @param	distance	Distance from target
		 * @param	angle	Shadow angle (degrees)
		 * @param	color	Shadow color in HEX (0x.....)
		 * @param	alpha	Shadow opacity
		 * @param	blur	Shadow blur
		 * @param	strength	Shadow spread
		 * @param	quality	Quality of shadow
		 * @param	inner	Inner shadow or not (true for inner)
		 * @param	knockout	Display only shadow (true or false)
		 * @param	hideObject	Hide object
		 */
		public static function DropShadow(target:DisplayObject, distance:Number=4, angle:Number=45, color:uint = 0x000000, alpha:Number = 0.8, blur:Number = 5, strength:Number=1, quality:int = 2, inner:Boolean=false, knockout:Boolean=false, hideObject:Boolean=false):void {
			var dropShadowFilter:DropShadowFilter = new DropShadowFilter(distance, angle, color, alpha, blur, blur, strength, quality, inner, knockout, hideObject);
			assignFilter(target, dropShadowFilter);
		}
		
		/**
		 * Remove filter from targeted object
		 * @param	target
		 */
		public static function Remove(target:DisplayObject):void {
			target.filters = [];
		}
		
		private static function assignFilter(target:DisplayObject, filter:*):void {
			var filterArr:Array = target.filters;
			filterArr.push(filter);
			target.filters = filterArr;
		}
	}
	
}